<template>
  <el-container>
    <el-header>
      <el-row :gutter="10">
        <el-col :span="4">
          <div class="grid-content bg-purple logo">
            <img src="@/assets/logo.png" alt>
          </div>
        </el-col>
        <el-col :span="16">
          <div class="grid-content bg-purple title">欢迎<span style="color:green;font-size: 24px;"> {{ username }}
            </span>电商后台管理系统</div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple logout">
            <el-button type="warning" @click="logout">退出</el-button>
          </div>
        </el-col>
      </el-row>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '174px'">
        <!-- 添加折叠div -->
        <div @click="isCollapse = !isCollapse" class="toggle-button">|||</div>
        <el-menu
        background-color="#333744"
         text-color="#fff"
          active-text-color="#409eff"
           :unique-opened="true"
           router
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="$route.path">
          <el-submenu v-for="item in menuList" :key="item.id" :index="item.path">
            <template slot="title">
              <i :class="iconsObj[item.id]" style="margin-right: 8px;"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item v-for="item1 in item.children" :key="item1.id" :index="`/` + item1.path">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ item1.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 菜单的路由跳转的内容会在此处显示，构成二级路由 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMenuListApi } from '@/api/user'
export default {
  data() {
    return {
      // 左侧菜单列表
      menuList: [],
      // 登录的用户信息
      username: window.sessionStorage.getItem('username'),
      // 一级分类的图标样式对象
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 控制菜单的折叠和展开,true为折叠，false为展开
      isCollapse: false
    }
  },
  created() {
    // console.log(this.$route)
    this.getMenuList()
  },
  methods: {
    // 获取左侧菜单栏
    async getMenuList() {
      const res = await getMenuListApi()
      if (res.meta.status !== 200) {
        return this.$message.error('数据获取失败')
      }
      this.menuList = res.data
    },
    // 退出登录
    logout() {
      window.sessionStorage.clear()
      this.$message({
        type: 'success',
        message: '退出成功',
        duration: 1000
      })
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="scss" scoped>
// 导入局部样式
@import './index.scss';
</style>